<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>配置管理后台</title>
    <style>
        * {
            margin: 0;
            padding: 0;
            box-sizing: border-box;
        }
        
        body {
            font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif;
            background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
            min-height: 100vh;
            display: flex;
            align-items: center;
            justify-content: center;
            padding: 20px;
        }
        
        .container {
            background: white;
            border-radius: 12px;
            box-shadow: 0 10px 40px rgba(0, 0, 0, 0.1);
            padding: 40px;
            max-width: 600px;
            width: 100%;
        }
        
        h1 {
            color: #333;
            margin-bottom: 10px;
            font-size: 28px;
        }
        
        .subtitle {
            color: #666;
            margin-bottom: 30px;
            font-size: 14px;
        }
        
        .form-group {
            margin-bottom: 25px;
        }
        
        label {
            display: block;
            margin-bottom: 8px;
            color: #333;
            font-weight: 500;
            font-size: 14px;
        }
        
        input[type="text"],
        input[type="password"] {
            width: 100%;
            padding: 12px 15px;
            border: 2px solid #e0e0e0;
            border-radius: 8px;
            font-size: 14px;
            transition: all 0.3s;
        }
        
        input[type="text"]:focus,
        input[type="password"]:focus {
            outline: none;
            border-color: #667eea;
            box-shadow: 0 0 0 3px rgba(102, 126, 234, 0.1);
        }
        
        .hint {
            font-size: 12px;
            color: #999;
            margin-top: 5px;
        }
        
        .button-group {
            display: flex;
            gap: 10px;
            margin-top: 30px;
        }
        
        button {
            flex: 1;
            padding: 12px 24px;
            border: none;
            border-radius: 8px;
            font-size: 14px;
            font-weight: 500;
            cursor: pointer;
            transition: all 0.3s;
        }
        
        .btn-primary {
            background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
            color: white;
        }
        
        .btn-primary:hover {
            transform: translateY(-2px);
            box-shadow: 0 5px 15px rgba(102, 126, 234, 0.4);
        }
        
        .btn-secondary {
            background: #f5f5f5;
            color: #666;
        }
        
        .btn-secondary:hover {
            background: #e0e0e0;
        }
        
        .message {
            padding: 12px 15px;
            border-radius: 8px;
            margin-bottom: 20px;
            font-size: 14px;
            display: none;
        }
        
        .message.success {
            background: #d4edda;
            color: #155724;
            border: 1px solid #c3e6cb;
        }
        
        .message.error {
            background: #f8d7da;
            color: #721c24;
            border: 1px solid #f5c6cb;
        }
        
        .current-config {
            background: #f8f9fa;
            padding: 15px;
            border-radius: 8px;
            margin-bottom: 25px;
        }
        
        .current-config h3 {
            font-size: 14px;
            color: #666;
            margin-bottom: 10px;
        }
        
        .config-item {
            font-size: 13px;
            color: #333;
            margin: 5px 0;
            word-break: break-all;
        }
        
        .config-item strong {
            color: #667eea;
        }
    </style>
</head>
<body>
    <div class="container">
        <h1>🔧 配置管理后台</h1>
        <p class="subtitle">管理应用的 Web URL 配置</p>
        
        <div id="message" class="message"></div>
        
        <div class="current-config" id="currentConfig">
            <h3>当前配置</h3>
            <div class="config-item"><strong>Web Host:</strong> <span id="currentWebhost">加载中...</span></div>
            <div class="config-item"><strong>WebView URL:</strong> <span id="currentWebviewUrl">加载中...</span></div>
            <div class="config-item"><strong>Addon Name:</strong> <span id="currentAddonName">加载中...</span></div>
            <div class="config-item"><strong>版本:</strong> <span id="currentVersion">加载中...</span></div>
        </div>
        
        <form id="configForm">
            <div class="form-group">
                <label for="webhost">Web Host URL</label>
                <input type="text" id="webhost" name="webhost" placeholder="http://qm.lr12.com" required>
                <div class="hint">后端服务器地址</div>
            </div>
            
            <div class="form-group">
                <label for="webviewUrl">WebView URL</label>
                <input type="text" id="webviewUrl" name="webviewUrl" placeholder="https://example.com/page" required>
                <div class="hint">应用内嵌网页的完整 URL</div>
            </div>
            
            <div class="form-group">
                <label for="addonName">Addon Name</label>
                <input type="text" id="addonName" name="addonName" placeholder="qm">
                <div class="hint">插件名称(可选)</div>
            </div>
            
            <div class="form-group">
                <label for="password">管理员密码</label>
                <input type="password" id="password" name="password" placeholder="请输入管理员密码" required>
                <div class="hint">默认密码: admin123</div>
            </div>
            
            <div class="button-group">
                <button type="button" class="btn-secondary" onclick="loadCurrentConfig()">刷新配置</button>
                <button type="submit" class="btn-primary">保存配置</button>
            </div>
        </form>
    </div>
    
    <script>
        // 配置服务器地址 - 根据实际情况修改
        const CONFIG_SERVER = 'http://localhost:3000';
        
        // 显示消息
        function showMessage(text, type) {
            const messageEl = document.getElementById('message');
            messageEl.textContent = text;
            messageEl.className = 'message ' + type;
            messageEl.style.display = 'block';
            
            setTimeout(() => {
                messageEl.style.display = 'none';
            }, 5000);
        }
        
        // 加载当前配置
        function loadCurrentConfig() {
            fetch(CONFIG_SERVER + '/api/config')
                .then(response => response.json())
                .then(data => {
                    if (data.errcode === 0) {
                        const config = data.data;
                        document.getElementById('currentWebhost').textContent = config.webhost || '-';
                        document.getElementById('currentWebviewUrl').textContent = config.webviewUrl || '-';
                        document.getElementById('currentAddonName').textContent = config.addon_name || '-';
                        document.getElementById('currentVersion').textContent = config.version || '0';
                        
                        // 填充表单
                        document.getElementById('webhost').value = config.webhost || '';
                        document.getElementById('webviewUrl').value = config.webviewUrl || '';
                        document.getElementById('addonName').value = config.addon_name || '';
                    } else {
                        showMessage('加载配置失败', 'error');
                    }
                })
                .catch(error => {
                    console.error('Error:', error);
                    showMessage('无法连接到配置服务器', 'error');
                });
        }
        
        // 保存配置
        document.getElementById('configForm').addEventListener('submit', function(e) {
            e.preventDefault();
            
            const formData = {
                webhost: document.getElementById('webhost').value,
                webviewUrl: document.getElementById('webviewUrl').value,
                addon_name: document.getElementById('addonName').value,
                adminPassword: document.getElementById('password').value
            };
            
            fetch(CONFIG_SERVER + '/api/admin/config', {
                method: 'POST',
                headers: {
                    'Content-Type': 'application/json',
                },
                body: JSON.stringify(formData)
            })
            .then(response => response.json())
            .then(data => {
                if (data.errcode === 0) {
                    showMessage('配置保存成功!', 'success');
                    document.getElementById('password').value = '';
                    loadCurrentConfig();
                } else {
                    showMessage(data.message || '保存失败', 'error');
                }
            })
            .catch(error => {
                console.error('Error:', error);
                showMessage('保存失败,请检查网络连接', 'error');
            });
        });
        
        // 页面加载时获取当前配置
        loadCurrentConfig();
    </script>
</body>
</html>

